package com.godme.rocket.utils;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;

import java.util.Objects;
import java.util.function.Supplier;

public class RateLimitHelper {


    public static <T> T apply(String rateLimitItem, boolean notRateLimit, Supplier<T> successSupplier, Supplier<T> failureSupplier){
        Entry entry = null;
        try{
            if(!notRateLimit){
                entry = SphU.entry(rateLimitItem);
            }
            return successSupplier.get();
        }catch (Exception e){
            return failureSupplier.get();
        }finally {
            if(Objects.nonNull(entry)){
                entry.exit();
            }
        }
    }


}
